#include "SystemUI.h"

const int USER_ADMIN = 0;
const int USER_NORMAL = 1;

void displayUserAllBookedMeetingRoom(const User &user)
{
    system("clear");
    user.listAllBookedMeetingRooms();
}

void displayUserCancelBookMeetingRoom(const User &user)
{
    system("clear");
    std::cout << "请输入要取消预定的会议室id：";
    int mid = SystemUtils::inputNumber();
    int cancelFlag = user.cancelMeetingRoom(mid);
    if (cancelFlag == Constant::CANCEL_SUCCESS)
    {
        std::cout << "恭喜您已经成功取消预定！" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
    else if (cancelFlag == Constant ::CANCEL_NOT_FOUND)
    {
        std::cout << "您输入的id 有误，该会议室不存在！" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
    else
    {
        std::cout << "不好意思，您输入的会议室不是您预定的，您无权取消预定！" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
}

void displayUserBookMeetingRoom(User &user)
{
    system("clear");
    std::cout << "请输入要预定的会议室id：";
    int mid = SystemUtils::inputNumber();
    int bookFlag = user.bookMeetingRoom(mid);
    if (bookFlag == Constant::BOOK_SUCCESS)
    {
        std::cout << "恭喜您已预定成功！" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
    else if (bookFlag == Constant::BOOK_NOT_FOUND)
    {
        std::cout << "不好意思，您预定的会议室不存在！" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
    else
    {
        std::cout << "您要预定的会议室已经被人占用，请预约别的会议室" << std::endl;
        std::cout << std::string(60, '-') << std::endl;
    }
}

void displayUserPage(User &user)
{

    bool flag = true;
    while (flag)
    {
        system("clear");
        std::cout << "欢迎 " << user.getDepartment() << " 的 " << user.getUsername() << std::endl;
        std::cout << "--------------------------------" << std::endl;
        std::cout << "1、预定一个会议室" << std::endl;
        std::cout << "2、取消预定会议室" << std::endl;
        std::cout << "3、列举所有已经预定的会议室" << std::endl;
        std::cout << "4、退出登录" << std::endl;
        std::cout << "--------------------------------" << std::endl;
        std::cout << "请输入您的操作：";
        int choice = SystemUtils::inputNumber();

        switch (choice)
        {
        case 1:
            displayUserBookMeetingRoom(user);
            break;
        case 2:
            displayUserCancelBookMeetingRoom(user);
            break;
        case 3:
            displayUserAllBookedMeetingRoom(user);
            break;
        case 4:
            std::cout << "再见! 欢迎下次使用" << std::endl;
            flag = false;
            break;
        default:
            break;
        }

        if (!flag)
        {
            break;
        }
        SystemUtils::waitInput();
    }
}

void displayRemoveMeetingRoomsStatus(const Admin &admin)
{
    system("clear");
    admin.clearAllMeetingRoomsStatus();
    std::cout << "清空所有会议室状态成功" << std::endl;
    std::cout << std::string(60, '-') << std::endl;
}

void displayShowAllMeetingRooms(const Admin &admin)
{
    system("clear");
    admin.showAllMeetingRooms();
}

void displayDeleteMeetingRoom(const Admin &admin)
{
    system("clear");
    std::cout << "请输入要删除的会议室id：";
    int id = SystemUtils::inputNumber();
    bool flag = admin.deleteMeetingRoom(id);
    if (flag)
    {
        std::cout << "删除成功 !" << std::endl;
    }
    else
    {
        std::cout << "删除失败，删除的会议室不存在，id = " << id << std::endl;
    }
}

void displayAddMeetingRoom(const Admin &admin)
{
    system("clear");
    std::cout << "请输入要添加的会议室id：";
    int id = SystemUtils::inputNumber();
    std::cout << "请输入要添加的会议室容量：";
    int capacity = SystemUtils::inputNumber();

    bool flag = admin.addMeetingRoom(id, capacity);
    if (flag)
    {
        std::cout << "添加成功 !" << std::endl;
    }
    else
    {
        std::cout << "添加失败，添加的会议室已经存在，id = " << id << std::endl;
    }
}

void displayAllUsers(const Admin &admin)
{
    system("clear");
    admin.showAllUsers();
}

void displayDeleteUser(const Admin &admin)
{
    system("clear");
    std::string username;
    std::cout << "请输入要删除的用户名：";
    std::cin >> username;
    bool flag = admin.deleteUser(username);
    if (flag)
    {
        std::cout << "删除用户【" << username << "】成功!!" << std::endl;
    }
    else
    {
        std::cout << "删除用户失败，用户【" << username << "】不存在" << std::endl;
    }
}

void displayAddUser(const Admin &admin)
{
    system("clear");
    std::string username;
    std::string password;
    std::string department;
    std::cout << "请输入要添加的用户名：";
    std::cin >> username;
    std::cout << "请输入用户的密码：";
    password = SystemUtils::getPassword(password);
    std::cout << std::endl;
    std::cout << "请输入用户的部门：";
    std::cin >> department;

    bool flag = admin.addUser(username, password, department);
    if (flag)
    {
        std::cout << "插入用户成功！" << std::endl;
    }
    else
    {
        std::cout << "插入失败！插入的用户名【" << username << "】已经存在" << std::endl;
    }
}

void displayAdminPage(const Admin &admin)
{
    bool flag = true;
    while (flag)
    {
        system("clear");
        std::cout << "欢迎尊贵的管理员【" << admin.getUsername() << "】！" << std::endl;
        std::cout << "--------------------------------" << std::endl;
        std::cout << "1、添加一个普通用户" << std::endl;
        std::cout << "2、删除一个普通用户" << std::endl;
        std::cout << "3、列举所有的普通用户" << std::endl;
        std::cout << "4、添加一个会议室" << std::endl;
        std::cout << "5、删除一个会议室" << std::endl;
        std::cout << "6、列举所有的会议室" << std::endl;
        std::cout << "7、重置会议室状态" << std::endl;
        std::cout << "8、退出登录" << std::endl;
        std::cout << "--------------------------------" << std::endl;
        std::cout << "请输入您的操作：";
        int choice = SystemUtils::inputNumber();

        switch (choice)
        {
        case 1:
            displayAddUser(admin);
            break;
        case 2:
            displayDeleteUser(admin);
            break;
        case 3:
            displayAllUsers(admin);
            break;
        case 4:
            displayAddMeetingRoom(admin);
            break;
        case 5:
            displayDeleteMeetingRoom(admin);
            break;
        case 6:
            displayShowAllMeetingRooms(admin);
            break;
        case 7:
            displayRemoveMeetingRoomsStatus(admin);
            break;
        case 8:
            std::cout << "再见! 欢迎下次使用" << std::endl;
            flag = false;
            break;
        default:
            break;
        }

        if (!flag)
        {
            break;
        }
        SystemUtils::waitInput();
    }
}

void showLoginPage(const int &flag)
{
    system("clear");
    if (USER_ADMIN == flag)
    {
        std::cout << "    管理员用户登录    " << std::endl;
    }
    else
    {
        std::cout << "     普通用户登录     " << std::endl;
    }
    std::cout << "----------------------" << std::endl;
    std::string username;
    std::string password;
    std::cout << "请输入用户名：";
    std::cin >> username;
    std::cout << "请输入密  码：";
    // std::cin >> password;
    password = SystemUtils::getPassword(password);
    std::cout << std::endl;
    // std::cout << "密码是" << password << std::endl;

    DataCenter dataCenter = DataCenter::getInstance();
    if (USER_ADMIN == flag)
    {
        Admin &admin = dataCenter.loginAdmin(username, password);
        if (&admin == &Admin::ERROR_ADMIN)
        {
            std::cout << "登录失败，返回上一层" << std::endl;
        }
        else
        {
            displayAdminPage(admin);
        }
    }
    else
    {
        User &user = dataCenter.loginUser(username, password);
        if (&user == &User::ERROR_USER)
        {
            std::cout << "登录失败，返回上一层" << std::endl;
        }
        else
        {
            displayUserPage(user);
        }
    }
}

void SystemUI::displayMainPage()
{
    bool flag = true;
    while (flag)
    {
        std::cout << "欢迎走进会议室预定系统" << std::endl;
        std::cout << "----------------------" << std::endl;
        std::cout << "1、普通用户登录" << std::endl;
        std::cout << "2、管理员用户登录" << std::endl;
        std::cout << "3、退出系统" << std::endl;
        std::cout << "----------------------" << std::endl;
        std::cout << "请输入您的选择：";

        int choice = SystemUtils::inputNumber();

        switch (choice)
        {
        case 1:
            // std::cout << "普通用户登录" << std::endl;
            showLoginPage(USER_NORMAL);
            break;
        case 2:
            // std::cout << "管理员用户登录" << std::endl;
            showLoginPage(USER_ADMIN);
            break;
        case 3:
            std::cout << "退出系统成功" << std::endl;
            flag = false;
        default:
            break;
        }
        if (!flag)
        {
            break;
        }
        SystemUtils::waitInput();
    }
}